unit Form_NewData;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Importer, Input, ZConnection, DB, ZAbstractRODataset,
  ZAbstractDataset, ZDataset,
  ZAbstractTable, ComCtrls, Setting;

type
  TNewData = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label6: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    Label35: TLabel;
    Label36: TLabel;
    TextNIK: TEdit;
    TextKK: TEdit;
    TextNama: TEdit;
    TextTempat: TEdit;
    TextIbu: TEdit;
    TextKelainan: TEdit;
    TextAlamat: TEdit;
    TextRW: TEdit;
    TextRT: TEdit;
    TextKeterangan: TEdit;
    cbKabkot: TComboBox;
    cbKec: TComboBox;
    cbDesa: TComboBox;
    cbJk: TComboBox;
    cbKawin: TComboBox;
    cbAgama: TComboBox;
    txtDusun: TEdit;
    Label26: TLabel;
    Label27: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    cbTgl: TComboBox;
    cbBulan: TComboBox;
    txtTahun: TEdit;
    txtUsia: TEdit;
    Label39: TLabel;
    Label40: TLabel;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TextNIKKeyPress(Sender: TObject; var Key: Char);
    procedure TextNamaKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure cbKabkotChange(Sender: TObject);
    procedure cbKecChange(Sender: TObject);
    procedure cbDesaChange(Sender: TObject);

  private
    procedure KeyOnlyNum(Sender: TObject; var Key: Char);
    function cekForm: Boolean;
    function getTgl: String;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  sKabkot: String;
  sKec: String;
  sDesa: String;

implementation

{$R *.dfm}

procedure TNewData.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TNewData.FormCreate(Sender: TObject);
var
  I: Integer;
  setting:TSetting_Form;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  with cbKawin do
  begin
    Clear;
    Items.Add('Belum');
    Items.Add('Sudah');
    Items.Add('Pernah');
  end;
  with cbAgama do
  begin
    Clear;
    Items.Add('Islam');
    Items.Add('Kristen');
    Items.Add('Katolik');
    Items.Add('Hindu');
    Items.Add('Budha');
    Items.Add('Kong Hu Cu');
  end;
  with cbJk do
  begin
    Clear;
    Items.Add('Laki');
    Items.Add('Perempuan');
  end;
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kabkot';
    Active := True;
    First;
  end;
  cbKabkot.Clear;
  while (not ZTable1.Eof) do
  begin
    cbKabkot.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
  cbTgl.Clear;
  cbTgl.items.Add(' ');
  for I := 1 to 31 do
    cbTgl.Items.Add(IntToStr(I));
  cbBulan.Clear;
  cbBulan.items.Add(' ');
  for I := 1 to 12 do
    cbBulan.Items.Add(IntToStr(I));
end;

Function cekRTRW(kata:String) : String;
var
  stemp:String;
begin
  sTemp:='';
  if (Length(kata)=1) then
    sTemp:='00'+kata
  else if (Length(kata)=2) then
    sTemp:='0'+kata
  else if (Length(kata)>3) then
    sTemp:=Copy(kata, Length(kata)-3, 3)
  else
    sTemp:=kata;
  Result := sTemp;
end;

Function TNewData.cekForm() : Boolean;
var
  sTemp:Boolean;
begin
  sTemp:=False;
  if ((TextNama.Text<>'') and (TextRW.Text<>'') and (TextRT.Text<>'') and (cbKabkot.ItemIndex>-1) and (cbKec.ItemIndex>-1) and (cbDesa.ItemIndex>-1) and (cbJk.ItemIndex>-1) and (cbKawin.ItemIndex>-1)) then begin
    if (txtUsia.Text<>'') then
      sTemp:=True
    else if ((cbTgl.ItemIndex>0) and (cbBulan.ItemIndex>0) and (txtTahun.Text<>'')) then
      sTemp:=True
  end;
  Result := sTemp;
end;

Function TNewData.getTgl() : String;
var
  sTemp,tgl,bln,thn:String;
  selisih:integer;
begin
  sTemp:='';
  if (txtUsia.Text<>'') then begin
    thn:=FormatDateTime('yyyy', Now());
    selisih:=StrToInt(thn)-StrToInt(txtUsia.Text);
    sTemp:='01-01-'+IntToStr(selisih);
  end
  else if ((cbTgl.ItemIndex>0) and (cbBulan.ItemIndex>0) and (txtTahun.Text<>'')) then begin
    tgl:=cbTgl.Items[cbTgl.ItemIndex];
    bln:=cbBulan.Items[cbBulan.ItemIndex];
    thn:=txtTahun.Text;
    sTemp:=tgl+'-'+bln+'-'+thn;
  end;
  Result := sTemp;
end;

procedure TNewData.Button1Click(Sender: TObject);
var
  TableName: TStringList;
  values: TStringList;
  Input: InputData;
  buttonSelected : Integer;
begin
  //prompt dulu
  buttonSelected := MessageDlg('Apakah anda yakin akan menambahkan data pemilih? Mohon dicek kembali data yang diisikan',mtConfirmation , mbOKCancel, 0);
  if buttonSelected = mrOK  then begin
    if (cekForm) then begin
      TableName := TStringList.Create;
      values := TStringList.Create;
      // format tablename nya seperti di bawah ini
      TableName.Add('DATA');
      TableName.Add('PENDUDUK');
      TableName.Add(sDesa);

      values.Add('0'); // dummy ID, no meaning
      // NIK`, `NKK`, `NAMA`, `TEMPAT`, `TANGGAL`, '+
      // '`IBU`, `AGAMA`, `KAWIN`, `KELAMIN`, `KELAINAN`, `ALAMAT`, `RW`, `RT`, `KET`, `ID_TANGGAL`
      values.Add(TextNIK.Text);
      values.Add(TextKK.Text);
      values.Add(AnsiUpperCase(TextNama.Text));
      values.Add(AnsiUpperCase(TextTempat.Text));
      values.Add(getTgl);
      values.Add(AnsiUpperCase(TextIbu.Text));
      values.Add(AnsiUpperCase(cbAgama.Items[cbAgama.ItemIndex]));
      values.Add(AnsiUpperCase(cbKawin.Items[cbKawin.ItemIndex]));
      values.Add(AnsiUpperCase(cbJk.Items[cbJk.ItemIndex]));
      values.Add(AnsiUpperCase(TextKelainan.Text));
      values.Add(AnsiUpperCase(TextAlamat.Text));
      values.Add(cbKabkot.Items[cbKabkot.ItemIndex]);
      values.Add(cbKec.Items[cbKec.ItemIndex]);
      values.Add(cbDesa.Items[cbDesa.ItemIndex]);
      values.Add(cekRTRW(TextRW.Text));
      values.Add(cekRTRW(TextRT.Text));
      values.Add(AnsiUpperCase(txtDusun.Text));
      values.Add(AnsiUpperCase(TextKeterangan.Text));

      //insert query
      Input.insertQuery(ZConnection1, ZTable1, ZQuery1, TableName, values);
      ShowMessage('Data pemilih tambahan telah berhasil dimasukkan. Untuk mengubahnya, silakan gunakan menu "ubah data pemilih" di "view data pemilih"');
      Close;
    end
      else
    ShowMessage('Cek jangan ada yang kosong pada : Nama Lengkap, Tanggal Lahir / Usia, Alamat (Terutama RT/RW), Status Pernikahan, Jenis Kelamin');
  end;
end;

procedure TNewData.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TNewData.cbDesaChange(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbDesa.Items[cbDesa.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'desa';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan) + ' and KODE_KECAMATAN = ' +
      QuotedStr(sKec);
    Filtered := True;
    Active := True;
    First;
    sDesa := FieldByName('KODE_DESA').AsString;
  end;
end;

procedure TNewData.cbKabkotChange(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kabkot';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan);
    Filtered := True;
    Active := True;
    First;
    sKabkot := FieldByName('KODE').AsString;
    // cb2
    Active := False;
    TableName := 'kecamatan';
    Filter := 'KODE_KABKOT = ' + QuotedStr(sKabkot);
    Filtered := True;
    Active := True;
    First;
  end;
  cbKec.Clear;
  cbDesa.Clear;
  while (not ZTable1.Eof) do
  begin
    cbKec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
end;

procedure TNewData.cbKecChange(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbKec.Items[cbKec.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kecamatan';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan) + ' and KODE_KABKOT = ' + QuotedStr
      (sKabkot);
    Filtered := True;
    Active := True;
    First;
    sKec := FieldByName('KODE_KECAMATAN').AsString;
    // cb2
    Active := False;
    TableName := 'desa';
    Filter := 'KODE_KECAMATAN = ' + QuotedStr(sKec);
    Filtered := True;
    Active := True;
    First;
  end;
  cbDesa.Clear;
  while (not ZTable1.Eof) do
  begin
    cbDesa.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
end;

procedure TNewData.KeyOnlyNum(Sender: TObject; var Key: Char);
begin
  if not(Key in [#8, '0' .. '9']) then
  begin
    Key := #0;
  end;
end;

procedure TNewData.TextNamaKeyPress(Sender: TObject; var Key: Char);
begin
  if not(Key in [#8, #32, #44 .. #46, #65 .. #90, #97 .. #122, '0' .. '9']) then
  begin
    Key := #0;
  end;
end;

procedure TNewData.TextNIKKeyPress(Sender: TObject; var Key: Char);
begin
  KeyOnlyNum(Sender, Key);
end;

end.

